<template>
  <div class="contain">
    <div class="bigbox">
      <img :src="bigimg" alt>
    </div>
    <p class="pro-type">{{userinfos.goodstype}}</p>
    <p class="pro-name">{{userinfos.goodsname}}</p>
    <p class="pro-id">款号:{{userinfos.id}}</p>
    <p class="pro-price">￥{{userinfos.goodsprice}}.00</p>
    <div class="pro-color">
      <p class="pro-color-1">颜色:</p>
      <div class="pro-flex">
        <div
          class="item"
          v-for="(item,index) in imglist"
          :key="index"
          @click="changeimg(item,index)"
          :class="{active:index==imgindexid}"
        >
          <img :src="item" alt>
        </div>
        <!-- <div class="item">
          <img
            src="https://img.fishfay.com/shopgoods/1/112135537R/112135537R-5-1/32d43cd4a067251769b569c7b0955990.jpg?x-image-process=image/resize,w_50,h_50"
            alt
          >
        </div>
        <div class="item">
          <img
            src="https://img.fishfay.com/shopgoods/1/112135537R/112135537R-2-1/53e0b92c287ba4bfacd6cbc911bd15c9.jpg?x-image-process=image/resize,w_50,h_50"
            alt
          >
        </div>
        <div class="item">
          <img
            src="https://img.fishfay.com/shopgoods/1/112135537R/112135537R-7-1/3a0b9d536458abea97208edabdfa99f1.jpg?x-image-process=image/resize,w_50,h_50"
            alt
          >
        </div>-->
      </div>
    </div>

    <div class="pro-size">
      <p class="size">尺码:</p>
      <div class="pro-size-flex">
        <div
          class="size-item"
          v-for="item in sizelist"
          :key="item.id"
          @click="changesize(item)"
          :class="{active:item.id==sizeindexid}"
        >{{item.size}}</div>
        <!-- <div class="size-item">40</div>
        <div class="size-item">40.5</div>
        <div class="size-item">41</div>
        <div class="size-item">42</div>
        <div class="size-item">42.5</div>
        <div class="size-item">43</div>
        <div class="size-item">44.5</div>-->
      </div>
    </div>

    <div class="pro-num">
      <p class="pro-num-1">购买数量</p>
      <div class="nums">
        <button class="add" v-if="buynum>1" @click="calc(-1)">-</button>
        <button class="add" v-else>-</button>
        <input type="number" class="inpt" v-model="buynum">
        <button class="add" @click="calc(1)">+</button>
      </div>
    </div>

    <div class="coupons" @click="tip">
      <span class="coupons-1">领取优惠卷</span>
      <span class="iconfont icon-fanhui-copy-copy"></span>
    </div>

    <div class="shopping_purchases">
      <div class="shopping" @click="addshoppingcar">加入购物车</div>
      <div class="purchases">立即购买</div>
    </div>
  </div>
</template>
<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
.active {
  border: 1px solid black !important;
}
.contain {
  margin-top: 54px;
}
.bigbox {
  width: 100%;
  min-height: 371px;
}
.bigbox img {
  width: 100%;
  height: 100%;
}
.pro-type {
  font-size: 12px;
  color: #888;
  padding: 0 10px;
}
.pro-name {
  line-height: 26px;
  margin: 5px 0;
  padding: 0 10px;
  font-size: 16px;
  color: #000;
}
.pro-id {
  font-size: 12px;
  color: #888;
  padding: 0 10px;
  line-height: 30px;
}
.pro-price {
  font-size: 16px;
  color: #000;
  padding: 0 10px;
  line-height: 32px;
}
.pro-color {
  padding: 10px 0;
  border-bottom: 1px solid #eee;
  border-top: 1px solid #eee;
}
.pro-color-1 {
  line-height: 20px;
  color: #000;
  font-size: 13px;
  margin-bottom: 3px;
  padding: 0 10px;
}
.pro-flex {
  width: 100%;
  padding: 0 10px;
  display: flex;
}
.pro-flex .item {
  width: 68px;
  height: 68px;
  margin-right: 10px;
  border: 1px solid #eee;
}
.item img {
  width: 100%;
  height: 100%;
}
.pro-size {
  padding: 10px 0;
  border-bottom: 1px solid #eee;
}
.size {
  line-height: 20px;
  color: #000;
  font-size: 13px;
  margin-bottom: 3px;
  padding: 0 10px;
}
.size-item {
  width: 68px;
  height: 34px;
  margin: 10px 10px 0 0;
  border: 1px solid #eee;
  text-align: center;
  line-height: 34px;
  color: #444;
}
.pro-size-flex {
  width: 100%;
  padding: 0 10px;
  display: flex;
  flex-wrap: wrap;
}
.pro-num {
  width: 100%;
  padding: 10px 10px;
  border-bottom: 1px solid #eee;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.pro-num-1 {
  font-size: 13px;
}
.add {
  border: none;
  border-radius: 2px;
  text-align: center;
  font-size: 16px;
  color: #666;
  background: #f4f4f4;
  height: 28px;
  width: 28px;
}
.inpt {
  width: 60px;
  height: 28px;
  outline: none;
  color: #666;
  font-size: 13px;
  text-align: center;
  border: 1px solid #ccc;
  margin: 0 10px;
}
.coupons {
  padding: 10px 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid #eee;
}
.coupons-1 {
  font-size: 13px;
  line-height: 28px;
}
.coupons .icon-fanhui-copy-copy {
  font-size: 20px;
  color: #000;
}
.shopping_purchases {
  width: 100%;
  padding: 20px 20px;
}
.shopping,
.purchases {
  width: 100%;
  height: 48px;
  border: 1px solid black;
  color: #000;
  font-size: 14px;
  text-align: center;
  line-height: 48px;
  margin-bottom: 10px;
}
.purchases {
  background-color: black !important;
  color: white !important;
}
</style>
<script>
import goodsinfosModel from "../model/goodsinfoModel";
import shoppingcarinfosModel from "../model/shoppingcarinfosModel";
import { mapMutations } from "vuex";
import { Toast } from "vant";
import { Dialog } from "vant";
export default {
  data() {
    return {
      userinfos: {},
      bigimg: "",
      imglist: [],
      imgindexid: 100,
      sizeindexid: 100,
      buynum: 1,
      size: null,
      smallimg: "",
      sizelist: [
        { id: 1, size: 39 },
        { id: 2, size: 40 },
        { id: 3, size: 40.5 },
        { id: 4, size: 41 },
        { id: 5, size: 42 },
        { id: 6, size: 42.5 },
        { id: 7, size: 43 },
        { id: 8, size: 44.5 }
      ]
    };
  },
  methods: {
    ...mapMutations(["setUserid"]),
    calc(n) {
      this.buynum = this.buynum + n;
    },
    tip() {
      Toast("没有可用的优惠卷");
    },
    changeimg(item, index) {
      this.bigimg = item.replace("w_50,h_50", "w_800,h_800");
      this.imgindexid = index;
      this.smallimg = item;
    },
    changesize(item) {
      this.sizeindexid = item.id;
      this.size = item.size;
    },
    async addshoppingcar() {
      if (!this.smallimg || !this.size) {
        Toast("请选择尺码和颜色");
      }
      if (this.smallimg && this.size) {
        //判断用户是否登录  是添加商品  不是去登录个人页面登录
        //  判断这个人是否加入过这个商品  加过更新数量  没有加过添加到数据库
        let userinfo = localStorage.getItem("username");

        if (userinfo) {
          userinfo = JSON.parse(userinfo);
          let userid = userinfo.id;
          let goodsid = this.$route.params.id;
          this.setUserid(userinfo.id);
          let { data: list2 } = await shoppingcarinfosModel.query({
            userid,
            goodsid
          });
          if (list2.length) {
            //更新
            let item = list2[0];
            console.log(item);

            await shoppingcarinfosModel.updata(item.id, {
              goodsbuynum: item.goodsbuynum * 1 + this.buynum * 1
            });
          } else {
            await shoppingcarinfosModel.add({
              goodsprice: this.userinfos.goodsprice,
              goodstype: this.userinfos.goodstype,
              goodsname: this.userinfos.goodsname,
              goodsbuynum: this.buynum,
              goodsimg: this.smallimg.replace("w_50,h_50", "w_200,h_200"),
              goodssize: this.size,
              userid,
              goodsid
            });
          }
          Dialog.confirm({
            title: "加入成功",
            message: "确定进入购物车 取消继续购物"
          })
            .then(() => {
              this.$router.push("/shoppingcar");
            })
            .catch(() => {
              // on cancel
              this.$router.push("/");
            });
        } else {
          this.$router.push("/mine");
        }
      }
    }
  },
  async created() {
    console.log(this.$route.params.id);
    let { data: list } = await goodsinfosModel.query({
      id: this.$route.params.id
    });
    console.log(list);
    if (list.length) {
      this.userinfos = list[0];
      this.bigimg = this.userinfos.goodsimg.replace(
        "w_300,h_300",
        "w_800,h_800"
      );
      console.log(this.bigimg);
      this.imglist = this.userinfos.goodslist;
      console.log(this.imglist);
      let str = this.imglist.split("|");
      this.imglist = str;
      console.log(this.imglist);
    } else {
      this.$router.push("/list");
    }
  }
};
</script>
